package com.chaofan.authsso.core.config;

import cn.hutool.core.util.StrUtil;
import com.chaofan.authsso.core.AuthSsoCore;
import com.chaofan.authsso.core.common.CacheType;
import com.chaofan.authsso.core.properties.SsoCoreProperties;
import com.chaofan.authsso.core.service.CacheInstance;
import com.chaofan.authsso.core.service.impl.LocalCacheInstanceImpl;
import com.chaofan.authsso.core.service.impl.RedisCacheInstanceImpl;
import com.chaofan.authsso.core.web.AuthController;
import com.chaofan.authsso.core.web.CheckController;
import com.chaofan.authsso.core.web.ErrorController;
import com.chaofan.authsso.core.web.TokenController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Author: Chaofan
 * Date: 2020/6/9 16:20
 * email: chaofan2685@qq.com
 */
public class BeanConfig {

    @Autowired
    SsoCoreProperties ssoCoreProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public CacheInstance cacheInstanceRegister(){
        if (StrUtil.equalsIgnoreCase(CacheType.REDIS.getType(),ssoCoreProperties.getCacheType())){
            return new RedisCacheInstanceImpl();
        }else if (StrUtil.equalsIgnoreCase(CacheType.LOCAL.getType(),ssoCoreProperties.getCacheType())){
            return new LocalCacheInstanceImpl();
        }else {
            return new LocalCacheInstanceImpl();
        }
    }

    @Bean
    public AuthController authControllerRegister(){
        return new AuthController();
    }

    @Bean
    public TokenController tokenControllerRegister(){
        return new TokenController();
    }

    @Bean
    public ErrorController errorControllerRegister(){
        return new ErrorController();
    }

    @Bean
    public CheckController checkControllerRegister(){
        return new CheckController();
    }

    @Bean
    public AuthSsoCore authSsoCoreRegister(){
        return new AuthSsoCore();
    }

    @Bean
    public RedisTemplate redisTemplateInit() {
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //TODO 设置序列化Value的实例化对象(会出问题,暂时不这样搞了)
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

}
